package com.jy.modules.cims.loan;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.data.common.dto.TCMmDictDTO;
import com.jy.modules.cims.data.dto.TbjDictDto;
import com.jy.modules.cims.loan.service.TBJDictService;
import com.jy.platform.core.message.DataMsg;
import com.jy.platform.core.message.PageParameter;
import com.jy.platform.core.message.PageUtil;
import com.jy.platform.core.message.QueryReqBean;
import com.jy.platform.restservice.exception.AbaboonException;
import com.jy.platform.restservice.web.base.BaseController;

/**
 * 
 * <b>类名称：</b>TbjDictController <br/>
 * <b>类描述：</b>铜板街字典维护类<br/>
 * <b>创建人：</b>zhangmi<br/>
 * <b>修改人：</b><br/>
 * <b>修改时间：</b>2016-7-27 下午2:45:41<br/>
 * <b>修改备注：</b><br/>
 * <b>版本信息：</b>v1.0.0<br/>
 */
@Controller
@Scope("prototype")
@RequestMapping("/tbjDictController")
public class TbjDictController extends BaseController{

    private static final Logger logger =  LoggerFactory.getLogger(TbjDictController.class);
    
	@Autowired
	@Qualifier("com.jy.modules.cims.loan.service.TBJDictService")
	private TBJDictService tbjDictService;
	
	@RequestMapping(value = "/prepareExecute/{operate}")
    public ModelAndView execute(@PathVariable("operate") String operate) throws AbaboonException {
        ModelAndView model = new ModelAndView();
        
        if("toQueryPage".equals(operate)){//跳转至 查询页面
        	model.setViewName("cims/tbjLoan/queryTbjDictDetail");
        }else if("toAdd".equals(operate)){
        	String dictCode = this.getParameterString("dictCode");
        	if(SimpleStringUtils.hasText(dictCode)){
            	TCMmDictDTO dictDto = tbjDictService.getTbjDictParentBydictCode(dictCode);
            	model.addObject("dto", dictDto);
        	}
        	model.setViewName("cims/tbjLoan/addTbjDictDetail");
        }else if("toUpdate".equals(operate)){
        	String id = this.getParameterString("id");
        	TbjDictDto tbjDictDto = tbjDictService.queryTbjDictById(id);
        	model.addObject("dto", tbjDictDto);
        	model.setViewName("cims/tbjLoan/updateTbjDictDetail");
        }
        return model;
    }
	/**
	 * 分页查询铜板街数据字典表
	 * @Title queryListTCMmDictDetail
	 * @Description TODO
	 * @param request
	 * @param dto
	 * @param dataMsg
	 * @return
	 * @throws Exception 
	 * DataMsg 
	 * @author zhangmi
	 * @date 2016-7-28-下午4:42:03
	 * @update 
	 * @throws
	 */
	@RequestMapping(value = "/queryListTbjDictDetail")
    @ResponseBody
    public DataMsg queryListTbjDictDetail(HttpServletRequest request, TbjDictDto dto, @ModelAttribute DataMsg dataMsg) throws Exception {
        
    	Map<String, Object> searchParams = new HashMap<String, Object>();
    	searchParams.put("dto", dto);
        QueryReqBean params = new QueryReqBean();
        params.setSearchParams(searchParams);
    	PageParameter pageInfo = PageUtil.toPageParameter(dataMsg);
		params.setPageParameter(pageInfo);
		
        List<TbjDictDto> list = tbjDictService.searchTbjDictByPaging(params.getSearchParams());
        
        dataMsg.setData(list);
        dataMsg.setTotalRows(pageInfo.getTotalCount());
        return dataMsg;
    }
	
	/**
	 * 铜板街字典维护新增功能
	 * @Title insertTbjDictDetail
	 * @Description TODO
	 * @param request
	 * @param dto
	 * @param dataMsg
	 * @return 
	 * DataMsg 
	 * @author zhangmi
	 * @date 2016-8-1-下午3:04:19
	 * @update 
	 * @throws
	 */
	@RequestMapping(value = "/insertTbjDictDetail")
	@ResponseBody
	public DataMsg insertTbjDictDetail(HttpServletRequest request, TbjDictDto dto, @ModelAttribute DataMsg dataMsg){
		try {
        	dto = (TbjDictDto)super.initDto(dto);
            tbjDictService.insertTbjDictDetail(dto);
            dataMsg = super.initDataMsg(dataMsg);
            dataMsg.setMsg("新增成功");
            dataMsg.setData(this.makeJSONData(dto.getId()));
        }catch (Exception e) {
        	dataMsg.failed(e.getMessage());
        	logger.error("执行方法insertTbjDictDetail异常：", e);
        }
        return dataMsg;
	}
	
	/**
	 * 铜板街字典维护修改功能
	 * @Title updateTbjDictDetail
	 * @Description TODO
	 * @param request
	 * @param dto
	 * @param dataMsg
	 * @return 
	 * DataMsg 
	 * @author zhangmi
	 * @date 2016-8-1-下午3:05:48
	 * @update 
	 * @throws
	 */
	@RequestMapping(value = "/updateTbjDictDetail")
    @ResponseBody
    public DataMsg updateTbjDictDetail(HttpServletRequest request, TbjDictDto dto, @ModelAttribute DataMsg dataMsg){
        try {
        	tbjDictService.updateTbjDictDetail(dto);
            dataMsg = super.initDataMsg(dataMsg);
            dataMsg.setMsg("修改成功");
            dataMsg.setData(this.makeJSONData(dto.getId()));
        }catch (Exception e) {
        	dataMsg.failed(e.getMessage());
            logger.error("执行方法updateTbjDictDetail异常：", e);
        }
        return dataMsg;
    }
	
	/**
	 * 铜板街字典维护删除功能
	 * @Title deleteTbjDictDetail
	 * @Description TODO
	 * @param request
	 * @param dataMsg
	 * @return 
	 * DataMsg 
	 * @author zhangmi
	 * @date 2016-8-1-下午3:10:03
	 * @update 
	 * @throws
	 */
	@RequestMapping(value = "/deleteTbjDictDetail")
    @ResponseBody
    public DataMsg deleteTbjDictDetail(HttpServletRequest request, @ModelAttribute DataMsg dataMsg){
    	
   	 	String ids = (String) this.getParameter("ids");//格式: 1,2,3
   	 	dataMsg = super.initDataMsg(dataMsg);
		try {
			tbjDictService.deleteTbjDictDetailById(ids);
			dataMsg.setMsg("删除成功");
		} catch (Exception e) {
			dataMsg.failed(e.getMessage());
			logger.error("执行方法deleteTbjDictDetail异常：", e);

		}
        return dataMsg;
    }
}
